<?php

if (!defined('BASEPATH'))
	exit('No direct script access allowed');

class Property extends CI_Controller {

	public function __construct() {
		parent::__construct();
		//check session
		$this->session->set_userdata(array('redirectUrl' =>current_url()));
		check_user_login();
	}

	// load index file
	function index() {
		$this->_display((object)array('output' => '' , 'js_files' => array() , 'css_files' => array()));
	}
	//get all the classifieds ads
	function view()
	{
		$crud = new grocery_CRUD();
		
		$crud->set_table('property');
		$crud->where('advertiserID',(int) $this->session->userdata('userid'));
		$crud->display_as('advertiserID', 'Advertiser Name');
		$crud->display_as('typeID', 'Property Type');
		$crud->display_as('item_condition', 'Condition');
//		$crud->display_as('address', 'Address');
		
		//$crud->set_relation('categoryID','adv_category','cat_descriptions');
		$crud->set_relation('advertiserID','masteruser','firstName');
		$crud->set_relation('typeID','property_type','type');
//		$crud->set_relation('address','property_address','{address1} {address2} {city}');

		$crud->set_subject('Property advertisements');
		$crud->columns('title','typeID','posted_date','expired_date');
		
		 /**
		$crud_columns('','','','') to set the number of columns display on list page
		$crud_fields('','','','') to set the number of columns display on details page
		$crud->display_as('lastName','Last Name'); to set display name
		**/
		$crud->field_type('categoryID','dropdown',
            array(1 => 'Sell', 2 => 'Buy',3 => 'Rent' , 4 => 'Long tenture'));
		$crud->field_type('is_negotiable','dropdown',
            array(0 => 'No', 1 => 'Yes'));
		$crud->field_type('price_denote','dropdown',
            array('KS' => 'KS', 'LAKH'=> 'LAKH','USD' => 'USD' , 'SGD' => 'SGD', 'CNY' => 'CNY'));

		$crud->unset_columns('Advertiser Name','contact_person','contact_email','contact_number','primary_image','small_image1','small_image2','small_image3');
		$crud->edit_fields('title','description','categoryID','typeID','address','contact_person','contact_number','contact_email','is_negotiable','price','price_denote','bed_room','bath_room','address1','address2','city','country','living_room','furnished','primary_image','small_image1','small_image2','small_image3');
		
		$crud->set_field_upload('primary_image','assets/uploads/property/');
		$crud->set_field_upload('small_image1','assets/uploads/property/');
		$crud->set_field_upload('small_image2','assets/uploads/property');
		$crud->set_field_upload('small_image3','assets/uploads/property');

		$crud->unset_add();
		
		$output = $crud->render();
		
		$crud->unset_jquery_ui();
		$this->_display($output);
	}
	//private function output
	function _display($output = null)
	{
		
		$this->load->view('admin/property/index.php',$output );	
	}

	// post new property advertisement (any categories)
	function post(){

		$data['title'] = 'Post property ad';
		$data['message'] = '';
		$this->load->model('User_model','',TRUE);
		$userID = (int) $this->session->userdata('userid');
		$data['user'] = $this->User_model->get_by_id($userID);
		$current_date = date('d-m-Y H:m:s');
		$data['expired_date'] = calculate_expiry_date($current_date, PROPERTY_RETENSION);
		$this->load->model('Propertycatg_model','',TRUE);
		$data['catg'] = $this->Propertycatg_model->get_property_categories();
		$data['action'] = site_url('admin/property/add');
		$this->load->view('admin/property/form', $data);
	}

	// post new property
	function add(){
		// set common properties
		$data['title'] = 'Post property ad';
		
		// run form_validation
		if ($this->form_validation->run('propertyfrm') == FALSE){
			$data['message'] = 'You have missed out to fill the following field';
			$data['action'] = site_url('admin/property/add');
			$data['user'] = null;
			$data['expired_date']  = $this->input->post('expired_date');
			$this->load->model('Propertycatg_model','',TRUE);
			$data['catg'] = $this->Propertycatg_model->get_property_categories();
			$data['selectedCatg'] = $adv['categoryID'];
			$this->load->view('admin/property/form', $data);
		}else{
			// save data
			// UI to add price as default 0.0
			$adv = array('title' => $this->input->post('ptitle'),
					'description' =>$this->input->post('pdescriptions'),
					'typeID' => $this->input->post('propType'),
					'price' => $this->input->post('pprice'),
					'contact_number' => $this->input->post('contact_no'),
					'contact_email' => $this->input->post('contact_email'),
					'contact_person'=> $this->input->post('contact_person'),
					'bath_room' => $this->input->post('bathrm'),
					'bed_room' => $this->input->post('bedrm'),
					'living_room' => $this->input->post('livingrm'),
					'categoryID' => (int) $this->input->post('category')
			);
			
			if ($this->input->post('propType') == '5'){
				$adv['features'] = $this->input->post('othcatg');
			}
			if ($this->input->post('address1'))
				$adv['address1']= $this->input->post('address1');
			if ($this->input->post('address2'))
				$adv['address2'] = $this->input->post('address2');
			if ($this->input->post('city'))
				$adv['city'] = $this->input->post('city');
			if ($this->input->post('country'))
				$adv['country'] = $this->input->post('country');

			if ($this->input->post('lat'))
				$adv['Lat'] = $this->input->post('lat');
			if ($this->input->post('lng'))
				$adv['Long'] = $this->input->post('lng');
				
			if ($this->input->post('price_denote'))
				$adv['price_denote']= $this->input->post('price_denote');	
			
			if ($this->input->post('squareft'))
				$adv['square_ft'] = $this->input->post('squareft');
			if ($this->input->post('floorno'))
				$adv['floor_no'] = $this->input->post('floorno');
			if ($this->input->post('facing'))
				$adv['facing'] = $this->input->post('facing');
			if ($this->input->post('furnished'))
				$adv['furnished'] = $this->input->post('furnished');
			if ($this->input->post('yearsold'))
				$adv['years_old'] = $this->input->post('yearsold');
			
			$adv['is_negotiable'] = $this->input->post('isnegotaible',TRUE)==null ? 0 : 1;
			
			$adv['advertiserID']=$this->session->userdata('userid');
			if (is_uploaded_file($_FILES['userfile']['tmp_name'][0])){
					$image_url = upload_files(450,300,'/property');
				//is_array ($image_url)/
			
				if($image_url){
					$urls = array_values($image_url);
					log_message('INFO', 'inside image url array' . $urls[0]);
					$adv['primary_image'] = isset($urls[0]) ? $urls[0] : null;
					$adv['small_image1'] = isset($urls[1]) ? $urls[1] : null;
					$adv['small_image2'] = isset($urls[2]) ? $urls[2] : null;
					$adv['small_image3'] = isset($urls[3]) ? $urls[3] : null;
					
				}else{
					$data = array('message' => 'upload problem' . $image_url, 'action' => 'admin/profile/add');
					$data['action'] = site_url('admin/property/add');
					$data['user'] = null;
					$data['expired_date']  = $this->input->post('expired_date');
					$this->load->model('Propertycatg_model','',TRUE);
					$data['catg'] = $this->Propertycatg_model->get_property_categories();
					$data['selectedCatg'] = $adv['categoryID'];
					$this->load->view('admin/property/form', $data);
				}
			}
			
			$this->load->model('Property_model','',TRUE);
			$id = $this->Property_model->save($adv);

			//deduct points
			//if ($id){
				//$this->load->model('Credit_model','',TRUE);
				//$this->Credit_model->decrease($adv->userID, 100);
			//}
			// set user  message
			$data['message'] = '<div class="success">Your advertisement has been successfully posted. Good Luck & Thanks for using MayKha as your partner. </div>';
		}
			
		// load view
		redirect('admin/property/view', $data);
		//$this->load->view('admin/property/view', $data);
	}

	/**
	 * Update
	 * @param unknown_type $propertyid
	 */
	function update ($propertyid) {

		// run form_validation
		if ($this->form_validation->run('propertyadv') == FALSE){
			$data['message'] = '';
		}else{
			//
			if (isset($propertyadv->price))
				$_POST['price'] = $propertyadv->price;

			if (isset($adv->contact_no))
				$_POST['contactno'] = $adv->contact_no;

			$_POST['posteddate'] = date('d-m-Y',strtotime($adv->posted_date));
			$_POST['expireddate'] = date('d-m-Y',strtotime($adv->expried_date));


			$data['title'] = 'Update an advertisement: General';
			$data['message'] = '';
			$data['action'] = site_url('advertisement/editAdv');

			$this->load->view('/includes/header', $data);
			$this->load->view('editadv_view');
			$this->load->view('/includes/footer');
		}

	}
}
?>
